<!DOCTYPE html>
<!--
/*
 * RSS2HTML - Updates a tab from a rss feed to html
 * Version: 0.2.0
 * Author: David Laurell <david.laurell@gmail.com>
 * License: GPLv3
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  The code to detect feeds on a page comes from Google's feed extension and therefore I must include this:
 *
 *   Copyright (c) 2010 The Chromium Authors. All rights reserved.
 *   Use of this source code is governed by a BSD-style license that can be
 *   found in the LICENSE file.
 */
-->
<html>
<head>
<script>
var feedPageUrl = chrome.extension.getURL('view.html');
var feedData = {};

chrome.extension.onRequest.addListener(
function(request, sender) {
  if (request.msg == "feedIcon") {
    feedData[sender.tab.id] = request.feeds;
	chrome.pageAction.setTitle({ tabId: sender.tab.id,
                               title: "Click for a menu of RSS feeds."});

	chrome.pageAction.show(sender.tab.id);
  }
});

chrome.tabs.onRemoved.addListener(function(tabId) {
	delete feedData[tabId];
});

chrome.tabs.onUpdated.addListener(
function(tabId, changeInfo, tab) {
	if(tab.status == "complete" && tab.url.indexOf(feedPageUrl) == -1) {
		var xhr = new XMLHttpRequest();
		xhr.open("GET", tab.url, true);
		xhr.setRequestHeader('Content-type', 'text/xml');
		xhr.onload = function() {
			if(xhr.getResponseHeader("Content-Type").indexOf("application/rss+xml") != -1 || 
				xhr.getResponseHeader("Content-Type").indexOf("application/atom+xml") != -1 ||
            (xhr.getResponseHeader("Content-Type").indexOf("text/xml") != -1 && (xmlparse.getElementsByTagName('rss')[0] || xmlparse.getElementsByTagName('feed')[0]))) {
				var newUrl = feedPageUrl + "?url=" + encodeURIComponent(tab.url);
				chrome.tabs.update(tabId, {url: newUrl});
			}
		}
		xhr.send(null);
	}
});
 
</script>
</head>
</html>
